﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GuavaData;

namespace MultiClassDBA.ChannelBuilder
{
    public partial class ColumnEditor : Form
    {
        private Scheme scheme;
        private Column current;

        public ColumnEditor(Scheme scheme)
        {
            this.scheme = scheme;
            InitializeComponent();
            foreach (Column c in scheme.columns)
                columnComboBox.Items.Add(c.Name);

        }

        private void enumeratedCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (enumeratedCheckBox.Checked)
                enumerationGroupBox.Enabled = true;
            else
                enumerationGroupBox.Enabled = false;
        }

        private void columnComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            current = null;
            foreach (Column c in scheme.columns)
                if (c.Name == columnComboBox.Text)
                    current = c;
            domainComboBox.Text = current.dom.ToString();
            enumeratedCheckBox.Checked = false;
            if (current.dom is GuavaData.Domain.Enumerated)
            {
                domainComboBox.Text = ((GuavaData.Domain.Enumerated)current.dom).Upcast().ToString();
                enumeratedCheckBox.Checked = true;
                foreach (String s in ((GuavaData.Domain.Enumerated)current.dom).Items)
                    enumerationListBox.Items.Add(s);
            }
            
            

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            switch (domainComboBox.Text)
            {
                case ("Boolean"):
                    current.dom = new GuavaData.Domain.Boolean();
                    break;
                case ("BigInteger"):
                    current.dom = new GuavaData.Domain.BigInteger();
                    break;
                case ("Integer"):
                    current.dom = new GuavaData.Domain.Integer();
                    break;
                case ("SmallInteger"):
                    current.dom = new GuavaData.Domain.SmallInteger();
                    break;
                case ("TinyInteger"):
                    current.dom = new GuavaData.Domain.TinyInteger();
                    break;
                case ("UniqueIdentifier"):
                    current.dom = new GuavaData.Domain.UniqueIdentifier();
                    break;
                //case ("Lookup"):
                    //current.dom = new GuavaData.Domain.Lookup();
                    //break;
                case ("Decimal"):
                    current.dom = new GuavaData.Domain.Decimal();
                    break;
                case ("Single"):
                    current.dom = new GuavaData.Domain.Single();
                    break;
                case ("Double"):
                    current.dom = new GuavaData.Domain.Double();
                    break;
                case ("String"):
                    current.dom = new GuavaData.Domain.String();
                    break;
                case ("DateTime"):
                    current.dom = new GuavaData.Domain.DateTime();
                    break;
                //case ("AnnotatedGuid"):
                    //current.dom = new GuavaData.Domain.AnnotatedGuid();
                   // break;
                default:
                    {
                    MessageBox.Show("Could not create datatype " + domainComboBox.Text);
                    return;
                    }
            }
            if (enumeratedCheckBox.Checked)
            {
                current.dom = new GuavaData.Domain.Enumerated();
                foreach (String s in enumerationListBox.Items)
                    ((GuavaData.Domain.Enumerated)current.dom).Items.Add(s);
            }
            this.Close();
        }

        private void addValueButton_Click(object sender, EventArgs e)
        {
            if (newEnumValueTextBox.Text == "")
                return;
            enumerationListBox.Items.Add(newEnumValueTextBox.Text);
            newEnumValueTextBox.Text = "";
        }

        private void removeValueButton_Click(object sender, EventArgs e)
        {
            enumerationListBox.Items.Remove(enumerationListBox.SelectedItem);
        }
    }
}
